package com.tangdada.whacmole.base;

import android.content.Context;
import android.media.MediaPlayer;

public class MediaPlayManager {

    private MediaPlayer mPlayer;
    private int mMediaResource;
    private Context context;

    public MediaPlayManager(Context context) {
        this.context = context;
        // mAudioManager = (AudioManager) context
        // .getSystemService(Context.AUDIO_SERVICE);
        // maxstreamVolume = mAudioManager
        // .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    public void restart() {
        if(mPlayer != null){
            if(mPlayer.isPlaying()) mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
        mPlayer = MediaPlayer.create(context, mMediaResource);
        mPlayer.setLooping(true);
        mPlayer.start();
    }

    public void start(int mediaResources, boolean loop) {
        mMediaResource = mediaResources;
        if(mPlayer != null){
            if(mPlayer.isPlaying()) mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
        mPlayer = MediaPlayer.create(context, mediaResources);
        mPlayer.setLooping(loop);
        mPlayer.start();
    }

    public void stop() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
    }

    public void resume() {
        if (mPlayer != null) {
            mPlayer.start();
        }
    }

}
